Checked এবং Unchecked Exception এর ধারণা

Java Technologies - Java.lang প্যাকেজ (Java.lang Package) - Exception Handling
310

Java তে Exceptions দুই ধরনের হয়: Checked Exceptions এবং Unchecked Exceptions। এই দুটি ধরণের এক্সসেপশন ব্যবস্থাপনা প্রক্রিয়া এবং কীভাবে এগুলি কোডে ব্যবহৃত হয় তার মধ্যে কিছু মৌলিক পার্থক্য রয়েছে।

1. Checked Exceptions:

Checked Exceptions হলো এমন ধরনের এক্সসেপশন, যেগুলি compile-time এ চেক করা হয় এবং এটি অবশ্যই handle (ধরা) করতে হবে বা declare (ঘোষণা) করতে হবে। যদি আপনি checked exception কে handle না করেন, তবে কম্পাইলার আপনাকে একটি compile-time error দিবে।

কীভাবে কাজ করে?

  • Checked exceptions সাধারণত এমন পরিস্থিতিতে ঘটে যেখানে কোনো অপারেশন ব্যর্থ হতে পারে, কিন্তু এটি সম্ভাব্যভাবে সমাধান করা যেতে পারে। যেমন, ফাইল পড়ার সময় বা নেটওয়ার্কের সাথে সংযোগ স্থাপন করার সময় ত্রুটি ঘটতে পারে, কিন্তু তা ঠিক করা সম্ভব।
  • যখন কোনো checked exception ঘটে, তখন এটি try-catch ব্লক দ্বারা ধরতে হয় বা throws কিওয়ার্ডের মাধ্যমে মেথড সিগনেচারে ডিক্লেয়ার করতে হয়।

Examples:

  • IOException
  • SQLException
  • ClassNotFoundException

উদাহরণ:

import java.io.*;

public class CheckedExceptionExample {
    public static void main(String[] args) {
        try {
            FileReader file = new FileReader("somefile.txt");
            BufferedReader reader = new BufferedReader(file);
            String line = reader.readLine();
            System.out.println(line);
        } catch (IOException e) {
            System.out.println("An IOException occurred: " + e.getMessage());
        }
    }
}

Explanation:

  • এখানে IOException একটি checked exception, তাই এটি try-catch ব্লক দ্বারা handle করা হয়েছে।

2. Unchecked Exceptions:

Unchecked Exceptions হল এমন এক্সসেপশন যা runtime এ ঘটে এবং এটি compile-time এ চেক করা হয় না। এগুলি RuntimeException এর সাবক্লাস। Unchecked exception গুলি সাধারণত programming errors এর কারণে ঘটে, যেমন null pointer access বা array index out of bounds

কীভাবে কাজ করে?

  • Unchecked exceptions ত্রুটি সৃষ্টিকারী কোডে আগেই উল্লেখ করা প্রয়োজন নয়, অর্থাৎ এটি explicitly handle করতে হবে না।
  • যেহেতু এগুলি runtime এ ঘটতে পারে, তাই try-catch ব্লক ব্যবহার করা বাধ্যতামূলক নয়, তবে এটি ব্যবহার করা যেতে পারে।

Examples:

  • NullPointerException
  • ArrayIndexOutOfBoundsException
  • ArithmeticException
  • IllegalArgumentException

উদাহরণ:

public class UncheckedExceptionExample {
    public static void main(String[] args) {
        int[] numbers = new int[5];
        
        try {
            // Accessing index beyond the array size
            System.out.println(numbers[10]); // This will throw ArrayIndexOutOfBoundsException
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("Caught an ArrayIndexOutOfBoundsException: " + e.getMessage());
        }
    }
}

Explanation:

  • এখানে ArrayIndexOutOfBoundsException একটি unchecked exception, যা runtime এ ঘটেছে। আমরা এটি try-catch ব্লকের মাধ্যমে handle করেছি, তবে এটি handle না করলেও compilation এর সময় কোন ত্রুটি হতো না।

Difference between Checked and Unchecked Exceptions:

FeatureChecked ExceptionUnchecked Exception
DefinitionExceptions that must be explicitly handled at compile time.Exceptions that are not checked at compile time.
SubclassSubclass of Exception (but not subclass of RuntimeException).Subclass of RuntimeException.
ExamplesIOException, SQLException, ClassNotFoundException.NullPointerException, ArrayIndexOutOfBoundsException, ArithmeticException.
Handling RequirementMust be handled with try-catch block or declared with throws.Optional handling (no compile-time check).
Compile-Time CheckYes, compile-time check is mandatory.No, compile-time check is not required.
When They OccurOccur due to external factors (e.g., I/O operations, database access).Occur due to bugs or logical errors in the program.
Performance OverheadMay add overhead due to mandatory handling.Generally does not cause significant performance overhead.

When to Use Checked and Unchecked Exceptions?

  • Use Checked Exceptions: When the error is recoverable and requires the caller to handle it explicitly. For example, errors in I/O operations, database connections, or network connections.
  • Use Unchecked Exceptions: When the error occurs due to a programming mistake or logical error that the programmer can prevent (e.g., NullPointerException, ArithmeticException). These should be used for bugs that should be fixed during development rather than handled in production.

Summary:

  • Checked Exceptions: Must be handled or declared, often occur due to external issues, and require the programmer to manage them explicitly.
  • Unchecked Exceptions: Do not need to be handled explicitly, often caused by programming errors, and typically indicate bugs in the code.
Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...